package hnevccom.wechatdemo;


import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    FindFragment findFragment;
    MyFragment myFragment;
    WeChatFragment weChatFragment;
    ContactsFragment contactsFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findFragment=new FindFragment();
        myFragment=new MyFragment();
        weChatFragment=new WeChatFragment();
        contactsFragment=new ContactsFragment();
    }
    public void buttonClick(View view){
        //切换不同的Fragment
        int id=view.getId();

        switch(id){
            case R.id.id_wechat:{
                //获取FragmentManager,通过getSupportFragmentManager()获取
                FragmentManager fm=getSupportFragmentManager();
                //开启事务，调用beginTransaction开启
                FragmentTransaction transaction=fm.beginTransaction();
                //向容器中添加(add)或替换（replace）Fragment；需要容器id
                transaction.replace(R.id.id_container,weChatFragment);
                //提交事务
                transaction.commit();
                break;
            }
            case R.id.id_contacts:{
                //获取FragmentManager,通过getSupportFragmentManager()获取
                FragmentManager fm=getSupportFragmentManager();
                //开启事务，调用beginTransaction开启
                FragmentTransaction transaction=fm.beginTransaction();
                //向容器中添加(add)或替换（replace）Fragment；需要容器id
                transaction.replace(R.id.id_container,contactsFragment);
                //提交事务
                transaction.commit();
                break;
            }
            case R.id.id_find:{
                //获取FragmentManager,通过getSupportFragmentManager()获取
                FragmentManager fm=getSupportFragmentManager();
                //开启事务，调用beginTransaction开启
                FragmentTransaction transaction=fm.beginTransaction();
                //向容器中添加(add)或替换（replace）Fragment；需要容器id
                transaction.replace(R.id.id_container,findFragment);
                //提交事务
                transaction.commit();
                break;
            }
            case R.id.id_my:{
                //获取FragmentManager,通过getSupportFragmentManager()获取
                FragmentManager fm=getSupportFragmentManager();
                //开启事务，调用beginTransaction开启
                FragmentTransaction transaction=fm.beginTransaction();
                //向容器中添加(add)或替换（replace）Fragment；需要容器id
                transaction.replace(R.id.id_container,myFragment);
                //提交事务
                transaction.commit();
                break;
            }
        }

    }
}
